/*
 * @Title:  PayNotifyUtil.java   
 * @Package com.shelai.util   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: 李凯     
 * @date:   2018年5月31日 下午5:58:55   
 * @version V1.0 
 */
package com.third.utils.pay;

import java.math.BigDecimal;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.third.base.wx.NotifyResult;
import com.third.utils.ali.AliPayUtil;
import com.third.utils.ali.AlipayConfig;
import com.third.utils.wx.WxPayConfig;
import com.third.utils.wx.WxPayUtil;

/*  
 * @ClassName:  PayNotifyUtil   
 * @Description:   支付回调解析验证工具
 * @author: 李凯
 * @date:   2018年5月31日 下午5:58:55   
 *     
 */
public class PayNotifyUtil{
	public static Logger log = LoggerFactory.getLogger(PayNotifyUtil.class);
	private static AlipayNotify alipayNotify;
	private static WxpayNotify wxpayNotify;
	private static PayNotifyUtil payNotifyUtil=new PayNotifyUtil();

	// 微信支付回掉处理
	public static WxpayNotify wxPayNotifyUtil(WxPayConfig wxConfig) {
		if (wxpayNotify == null) {
			wxpayNotify = payNotifyUtil.new WxpayNotify(wxConfig);
		}
		return wxpayNotify;
	}

	// 支付宝支付回调处理
	public static AlipayNotify aliPayNotifyUtil(AlipayConfig aliConfig) {
		if (alipayNotify == null) {
			alipayNotify =payNotifyUtil.new AlipayNotify(aliConfig);
		}
		return alipayNotify;
	}

	public class AlipayNotify {
		private AlipayConfig aliConfig;// 支付宝配置
		
		private AlipayNotify(AlipayConfig aliConfig) {
			super();
			this.aliConfig = aliConfig;
		}

		/*
		 * @Description:支付宝支付回调处理
		 * 
		 */
		public NotifyResult alipayNotify(HttpServletRequest request) {
			NotifyResult notifyResult = new NotifyResult();
			try {
				AliPayUtil aliPay = AliPayUtil.create(aliConfig);
				Map<String, String> map = aliPay.processNotify(request);
				notifyResult.setTradeStatus(true);
				notifyResult.setAppId(map.get("app_id"));
				notifyResult.setAmount(new BigDecimal(map.get("receipt_amount")));
				notifyResult.setAttach(map.get("passback_params"));
				notifyResult.setOutTradeNo(map.get("out_trade_no"));
				notifyResult.setTradeNo(map.get("trade_no"));
			} catch (Exception e) {
				log.error("支付宝支付回调处理异常：" + e.getMessage());
				notifyResult.setTradeStatus(false);
				;
			}

			return notifyResult;
		}
	}

	public class WxpayNotify {
		private WxPayConfig wxPayConfig;// 微信支付配置
		
		private WxpayNotify(WxPayConfig wxPayConfig) {
			super();
			this.wxPayConfig = wxPayConfig;
		}

		/*
		 * @Description:微信支付回调处理
		 * 
		 */
		public NotifyResult wxpayNotify(HttpServletRequest request) {
			NotifyResult notifyResult = new NotifyResult();
			try {
				WxPayUtil wxpay = WxPayUtil.create(wxPayConfig);
				Map<String, String> dataMap = wxpay.processNotify(request);
				notifyResult.setTradeStatus(true);
				notifyResult.setAppId(dataMap.get("appid"));
				notifyResult.setAmount(new BigDecimal(dataMap.get("cash_fee")).divide(new BigDecimal("100")).setScale(2,
						BigDecimal.ROUND_HALF_UP));
				notifyResult.setAttach(dataMap.get("attach"));
				notifyResult.setOutTradeNo(dataMap.get("out_trade_no"));
				notifyResult.setTradeNo(dataMap.get("transaction_id"));

			} catch (Exception e) {
				log.error("微信支付回调处理异常：" + e.getMessage());
				notifyResult.setTradeStatus(false);
				;
			}
			return notifyResult;
		}

	}

}
